#include "touch_panel.h"
enum direction touchPanel(int tp)
{
    int x1, y1;
    int x2, y2;

    bool x1done = false;
    bool y1done = false;

    struct input_event buf;
    while(1)
    {
        bzero(&buf, sizeof(buf));
        read(tp, &buf, sizeof(buf));

        // get first pos
        if(buf.type == EV_ABS && buf.code == ABS_X && !x1done)
        {
            x2 = x1 = buf.value;
            x1done = true;
        }
        if(buf.type == EV_ABS && buf.code == ABS_Y && !y1done)
        {
            y2 = y1 = buf.value;
            y1done = true;
        }

        if(buf.type == EV_ABS && buf.code == ABS_X)
            x2 = buf.value;
        if(buf.type == EV_ABS && buf.code == ABS_Y)
            y2 = buf.value;

        if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 0)
        {
            if(abs(x1-x2)<=20 && abs(y1-y2)<=20)
            {
                printf("(%d,%d)",x1,x2);
                return click;
            }
            else if(x1-x2>30&&abs(y1-y2)<50)
            {
                return left;
            }
            else if(x2-x1>30&&abs(y1-y2)<50)
            {
                return right;
            }
            else if(y1-y2>30&&abs(x1-x2)<50)
            {
                return up;
            }
            else
            {
                x1done = false;
                y1done = false;
                continue;
            }
        }
    }
}

// int main(int argc, char const *argv[])
// {
//     int tp = open("/dev/input/event0", O_RDWR);

//     while(1)
//     {
//         enum direction dir=touchPanel(tp);
//         switch (dir)
//         {
//             case click:
//             printf("点击\n");break;
//             case right:
//             printf("右滑\n");break;
//             case left:
//             printf("左滑\n");break;
//             default:
//             break;
//         }
        
//     }

//     return 0;
// }